﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.rms.roll_file
{
    public partial class query : ModulePage
    {
        public string GetCodeName(string codeId, string codeType)
        {
            if (codeType == "Secret")
            {
                return new RmsCodeDao().GetCodeName(base.Conn, codeId, RmsCodeDao.CodeType.Secret);
            }
            if (codeType == "Urgency")
            {
                return new RmsCodeDao().GetCodeName(base.Conn, codeId, RmsCodeDao.CodeType.Urgency);
            }
            return string.Empty;
        }
        public string GetRollName(int rollId)
        {
            return new RmsRollDao().GetRollName(base.Conn, rollId);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.ddlSecret.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Secret);
                this.ddlSecret.DataBind();
                this.ddlSecret.Items.Insert(0, new ListItem("", ""));
                this.ddlUrgency.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Urgency);
                this.ddlUrgency.DataBind();
                this.ddlUrgency.Items.Insert(0, new ListItem("", ""));
                this.ddlFileKind.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileKind);
                this.ddlFileKind.DataBind();
                this.ddlFileKind.Items.Insert(0, new ListItem("", ""));
                this.ddlFileType.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileType);
                this.ddlFileType.DataBind();
                this.ddlFileType.Items.Insert(0, new ListItem("", ""));
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }
            if (this.Page.IsPostBack)
            {
                this.ViewState["CurrentPage"] = 1;
                this.litNavTitle.Text = "文件查询结果";
                this.pnlMain.Visible = false;
                SqlCommand command = new SqlCommand();
                command.Connection = base.Conn;
                ArrayList list = new ArrayList();
                string commandText = "SELECT [FileId],[RollId],[FileCode],[FileTitle],[SendUnit],[SendDate],[Secret] FROM [dbo].[Rms_File] WHERE [DelUser] IS NULL AND ([RollId] IS NULL OR [RollId] IN(SELECT [RollId] FROM [dbo].[Rms_Roll] WHERE [Status] = 0))  ";
                if (this.txtFileCode.Text != string.Empty)
                {
                    commandText = commandText + " AND [FileCode] LIKE '%' + @FileCode + '%' ";
                    list.Add(new object[] { "@FileCode", this.txtFileCode.Text });
                }
                if (this.txtFileSubject.Text != string.Empty)
                {
                    commandText = commandText + " AND [FileSubject] LIKE '%' + @FileSubject + '%' ";
                    list.Add(new object[] { "@FileSubject", this.txtFileSubject.Text });
                }
                if (this.txtFileTitle.Text != string.Empty)
                {
                    commandText = commandText + " AND [FileTitle] = @FileTitle ";
                    list.Add(new object[] { "@FileTitle", this.txtFileTitle.Text });
                }
                if (this.txtFileTitle0.Text != string.Empty)
                {
                    commandText = commandText + " AND [FileTitle0] LIKE '%' + @FileTitle0 + '%' ";
                    list.Add(new object[] { "@FileTitle0", this.txtFileTitle0.Text });
                }
                if (this.txtSendUnit.Text != string.Empty)
                {
                    commandText = commandText + " AND [SendUnit] = @SendUnit ";
                    list.Add(new object[] { "@SendUnit", this.txtSendUnit.Text });
                }
                if (this.txtSendDateStart.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendDate] >= @SendDateStart ";
                    list.Add(new object[] { "@SendDateStart", Convert.ToDateTime(this.txtSendDateStart.Value + " 00:00:00") });
                }
                if (this.txtSendDateEnd.Value != string.Empty)
                {
                    commandText = commandText + " AND [SendDate] <= @SendDateEnd ";
                    list.Add(new object[] { "@SendDateEnd", Convert.ToDateTime(this.txtSendDateEnd.Value + " 23:59:59") });
                }
                if (this.ddlSecret.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Secret] = @Secret ";
                    list.Add(new object[] { "@Secret", this.ddlSecret.SelectedValue });
                }
                if (this.ddlUrgency.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Urgency] = @Urgency ";
                    list.Add(new object[] { "@Urgency", this.ddlUrgency.SelectedValue });
                }
                if (this.ddlFileKind.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [FileKind] = @FileKind ";
                    list.Add(new object[] { "@FileKind", this.ddlFileKind.SelectedValue });
                }
                if (this.ddlFileType.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [FileType] = @FileType ";
                    list.Add(new object[] { "@FileType", this.ddlFileType.SelectedValue });
                }
                if (this.txtFilePage.Text != string.Empty)
                {
                    commandText = commandText + " AND [FilePage] = @FilePage ";
                    list.Add(new object[] { "@FilePage", this.txtFilePage.Text });
                }
                if (this.txtPrintPage.Text != string.Empty)
                {
                    commandText = commandText + " AND [PrintPage] = @PrintPage ";
                    list.Add(new object[] { "@PrintPage", this.txtPrintPage.Text });
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    commandText = commandText + " AND [Remark] LIKE '%' + @Remark + '%' ";
                    list.Add(new object[] { "@Remark", this.txtRemark.Text });
                }
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray[0].ToString(), objArray[1]);
                }
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.pnlResult.Visible = true;
                    this.rptRmsFile.DataSource = table;
                    this.rptRmsFile.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("无符合条件的文件");
                }
            }


        }
    }
}
